<?php


namespace App\Http\Model\Job;


use App\Enum\AccountNettingEnum;
use App\Enum\ClassListPayResultHistoryEnum;
use Illuminate\Support\Facades\DB;

class PfsHeadQueryWithdrawProvisionModel
{

    /**
     * 查询监管账户数据是否全部推送成功
     *
     * @param $pfsId
     * @param $createDate
     *
     * @return bool
     */
    public static function payResultHistorySuccess($pfsId, $createDate)
    {
        /*检查是否存在推送失败的记录*/
        $exist = DB::select('SELECT `id` FROM `class_list_pay_result_history` WHERE `pfs_dw_id`=:pfs_dw_id AND `create_date`=:create_date AND `result`!=:result', [
            'pfs_dw_id'   => $pfsId,
            'create_date' => $createDate,
            'result'      => ClassListPayResultHistoryEnum::RESULT_SUCCESS,
        ]);
        return empty($exist) ? true : false;
    }


    /**
     * 备份金退费借回结果查询-更新记录
     *
     * @param $data
     * @param $result
     *
     * @throws \Exception
     */
    public static function ins($data, $result)
    {
        $message = empty($result->message) ? '' : $result->message;
        /*处理未完成*/
        if (empty($result->code) || $result->code !== 200) {
            DB::table('account_netting')->where('id', $data['id'])->update([
                'pull_time' => date('Y-m-d H:i:s'),
                'pfs_desc'  => $message,
            ]);
            return 0;
        }
        if (empty($result->data)) {
            throw new \Exception('备份金退费借回结果查询-招行返回数据异常');
        }
        $desc = empty($result->data->desc) ? '' : $result->data->desc;
        /*处理完成*/
        DB::table('account_netting')->where('id', $data['id'])->update([
            'pull_time'           => date('Y-m-d H:i:s'),
            'pfs_desc'            => empty($desc) ? $message : $desc,
            'pfs_tran_serial'     => empty($result->data->tranSerial) ? '' : $result->data->tranSerial,
            'pfs_transfer_status' => $result->data->transferStatus,
        ]);
    }


    /**
     * 需要执行查询的划拨单号
     *
     * @param $pullTime
     *
     * @return array
     */
    public static function listTransferId($pullTime)
    {
        return DB::select('SELECT `id`,`pfs_dw_id`,`transfer_id` FROM `account_netting` WHERE `pull_time`>:pull_time AND `pfs_transfer_status`=:pfs_transfer_status AND `is_del`=0 ORDER BY `ctime` DESC', [
            'pull_time'           => $pullTime,
            'pfs_transfer_status' => AccountNettingEnum::PFS_A,
        ]);
    }

}





/**
 * PfsHeadQueryWithdrawProvisionModel.php
 *
 * 说明:
 *
 * 修改历史
 * ----------------------------------------
 * 2021/6/9 14:30:00   操作:创建
 **/
